package com.ehotting.edsta.diagnosisPredict.controller;

import com.ehotting.eaf.core.domain.R;
import com.ehotting.eaf.core.web.domain.diagnosisPredict.PageDto;
import com.ehotting.edsta.diagnosisPredict.domain.dto.AlgoVersionFile;
import com.ehotting.edsta.diagnosisPredict.feign.AlgoVersionFileFeignService;
import com.ehotting.edsta.diagnosisPredict.utils.CommonUtils;
import feign.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 轻量化算法参数历史版本
 */
@RestController
@RequestMapping("/algoVersionFile")
@Api(tags = "AlgoVersionFileController",description = "轻量化算法参数历史版本")
public class AlgoVersionFileController {

    @Autowired
    private AlgoVersionFileFeignService algoVersionFileFeignService;

    /**
     * 列表
     * @param counts
     * @param fileAlias
     * @param fileName
     * @param fileSize
     * @param fileUrl
     * @param id
     * @param templateId
     * @param updateTime
     * @param version
     * @return
     */
    @GetMapping("/pageList")
    @ApiOperation("列表")
    public R<PageDto<AlgoVersionFile>> pageList(@RequestParam(name = "counts",required = false) Integer counts,
                                                @RequestParam(name = "fileAlias",required = false) String fileAlias,
                                                @RequestParam(name = "fileName",required = false) String fileName,
                                                @RequestParam(name = "fileSize",required = false) String fileSize,
                                                @RequestParam(name = "fileUrl",required = false) String fileUrl,
                                                @RequestParam(name = "id",required = false) Integer id,
                                                @RequestParam(name = "templateId",required = false) Integer templateId,
                                                @RequestParam(name = "updateTime",required = false) Date updateTime,
                                                @RequestParam(name = "version",required = false) String version) {
        return R.convert(algoVersionFileFeignService.pageList(counts, fileAlias, fileName, fileSize,
                fileUrl, id, templateId, updateTime, version));
    }

    /**
     * 单个/批量版本删除
     * @param ids
     * @return
     */
    @GetMapping("/deleteByIds")
    @ApiOperation("单个/批量版本删除")
    public R<String> deleteByIds(String ids) {
        return R.convert(algoVersionFileFeignService.deleteByIds(ids));
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation("根据ID查询")
    public R<AlgoVersionFile> getById(Long id) {
        R<AlgoVersionFile> convert = R.convert(algoVersionFileFeignService.getById(id));
        return convert;
    }

    /**
     * 上传
     * @param file
     * @param id
     * @param templateId
     * @param version
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("上传")
    public R<AlgoVersionFile> upload(@RequestParam("file") MultipartFile file,
                                     @RequestParam("id") Long id,
                                     @RequestParam("templateId") Long templateId,
                                     @RequestParam("version") String version) {
        return R.convert(algoVersionFileFeignService.upload(file,id,templateId,version));
    }

    /**
     * 生成下载文件
     * @param id
     * @return
     */
    @GetMapping("/download")
    @ApiOperation("生成下载文件")
    public void download(@RequestParam("id") Long id, HttpServletResponse response) {
        Response download = algoVersionFileFeignService.download(id);
        CommonUtils.downloadFileFromFeign(response,"",download);
    }

}